// @Date         2021-12-09
// @Author       Aaron Zhang
// @Description
// @FileName      api_error.go

package api_error

type ApiError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func NewApiError(code int) *ApiError {
	return &ApiError{
		Code:    code,
		Message: getMsg(code),
		Data:    nil,
	}
}

// NewApiErrorCustomer 自定义错误码和消息
func NewApiErrorCustomer(code int, msg string) *ApiError {
	return &ApiError{
		Code:    code,
		Message: msg,
	}
}

func (a *ApiError) Error() string {
	return a.Message
}

func (a *ApiError) SetErrCode(code int) *ApiError {
	return &ApiError{
		Code:    code,
		Message: getMsg(a.Code),
		Data:    a.Data,
	}
}

func (a *ApiError) SetData(data interface{}) *ApiError {
	return &ApiError{
		Code:    a.Code,
		Message: getMsg(a.Code),
		Data:    data,
	}
}

func (a *ApiError) SetMessage(msg string) *ApiError {
	return &ApiError{
		Code:    a.Code,
		Message: msg,
		Data:    a.Data,
	}
}

func (a *ApiError) Response() *ApiError {
	return &ApiError{
		Code:    a.Code,
		Message: getMsg(a.Code),
		Data:    a.Data,
	}
}
